package org.flow.basic.thread.timer;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * @author xl-9527
 * @since 2024/8/11
 **/
public class ExecutorScheduledPoolTestDemo implements ScheduledTestDemo<Runnable> {

    private ScheduledExecutorService service;

     public ScheduledExecutorService getScheduledExecutor(String taskName) {
         if (service == null) {
             service = Executors.newScheduledThreadPool(3, runnable -> new Thread(runnable, taskName));
         }
         return service;
     }

    @Override
    public void newTask(final Runnable task, final String taskName) {
        final ScheduledExecutorService scheduledExecutor = getScheduledExecutor(taskName);
        scheduledExecutor.schedule(task, 1, TimeUnit.SECONDS);
    }
}
